package com.kfm.sendmsg.util;

import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.Common;

public class SendMessage {

    // kfm01@1634502866574392.onaliyun.com  kaifamiao001
    // 阿里云平台的 AccessKey ID 及 AccessKey Secret，WEB项目可配置到配置文件中
    private static final String AccessKey = "LTAI5tEgptNNwaLjUAeuPq7m";

    private static final String AccessKey_Secret = "F67ANe7miS24re7YL70BWUeVdZ52n9";



    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    public static String send(String phone, String code) throws Exception {
        com.aliyun.dysmsapi20170525.Client client = createClient(AccessKey, AccessKey_Secret);
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setSignName("EShop")
                .setTemplateCode("SMS_180355535")
                .setPhoneNumbers(phone)
                .setTemplateParam("{\"code\":\"" + code + "\"}");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse response = client.sendSmsWithOptions(sendSmsRequest, runtime);
            System.out.println(response.getBody().message);
            return response.getBody().message;
        } catch (TeaException error) {
            // 如有需要，请打印 error
            System.err.println(Common.assertAsString(error.message));
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            System.err.println(Common.assertAsString(error.message));
        }
        return null;
    }

    public static void main(String[] args_) throws Exception {
        send("15091337009", "123456");
    }
}
